<?php

namespace App\Http\Util;

use App\Models\AllExam;
use App\Models\Images;
use App\Models\QuestionCategory;
use http\Env\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Image;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as ThumbImage;

class Upload
{
//    const MAIN_URL = 'http://demo.exam.com';
      const MAIN_URL = '';


//    public function uploadImg($inputData){
//        $rules = [
//            'file' => [ 'file','image','max:10240' ]
//        ];
//        $validator = Validator::make($inputData,$rules);
//        if($validator->fails()){
//            return '';
//        }
//        $photo = $inputData['file'];
//        $file_name = uniqid().'.'.$photo->getClientOriginalExtension();
//        $file_relative_path = 'assess/upload/images/'.date('Y-m-d');
//        $file_path = public_path($file_relative_path);
//        if (!is_dir($file_path)){
//            mkdir( $file_path);
//        }
//        $imgBig = \Intervention\Image\Facades\Image::make(file_get_contents($photo->getRealPath()));
//        $thumbnail_file_path = $file_path . '/thumbnail-'.$file_name;
//        $big_file_path = $file_path . '/thumbnail-'.$file_name;
//        $image = Image::make($photo)->resize(200, null, function ($constraint) {$constraint->aspectRatio();})->save($thumbnail_file_path);
//        $imgBigWidth = $imgBig->width();
//        $imgBigHeight = $imgBig->height();
//        $imgBig->resize(null, 854, function ($constraint) {$constraint->aspectRatio();})->save($big_file_path);
//        $imgBig->resize(null, 394, function ($constraint) {$constraint->aspectRatio();})->save($thumbnail_file_path);
//        $data['big'] =  $file_relative_path . '/big-'.$file_name ;
//        return $file_relative_path. ''.'/thumbnail-'.$file_name;
//    }

    // 上传pdf 文件
//
//    public function uploadPdf($inputData){
//        $pdf = $inputData['file'];
//        //原文件名
//        $originalName = $pdf->getClientOriginalName();
//        //扩展名
//        $ext = $pdf->getClientOriginalExtension();
//        $type = $pdf->getClientMimeType();
//        if($type != 'application/pdf'){
//            return '';
//        }
//        //临时绝对路径
//        $realPath = $pdf->getRealPath();
//        $filename = 'agreement'.'.'.$ext;
//        $bool = Storage::disk('uploadPdf')->put($filename,file_get_contents($realPath));
//        return  self::MAIN_URL .'assess/upload/pdf'.'/'.$filename;
//    }
//
    /**上传首页轮播
     * @param $inputData
     * @return array
     * @throws \Exception
     *
     */
    public function uploadSlide($inputData){
        try{
            $file = $inputData['file'];
            //原文件名
            $originalName = $file->getClientOriginalName();
            //扩展名
            $ext = $file->getClientOriginalExtension();
            $type = $file->getClientMimeType();
            //临时绝对路径
            $realPath = $file->getRealPath();
            $filename = 'banner_'.uniqid().time().'.'.$ext;
            $thumb_path = public_path('/upload/banner/thumb/');
            $thumb_name =$thumb_path.$filename;
            $bool = Storage::disk('uploadSlide')->put($filename,file_get_contents($realPath));
            $data['url']  ='/upload/banner/'.$filename;
            $imgBig = \Intervention\Image\Facades\Image::make(file_get_contents($realPath));
            $imgBigWidth = $imgBig->width();
            $imgBigHeight = $imgBig->height();
            $imgBig->resize(200*($imgBigWidth/$imgBigHeight), 200)->save($thumb_name);
            $data['thumb'] = '/upload/banner/thumb/'.$filename;
            return $data;
        }catch(\Exception $e){
            throw $e;
        }
    }

    /**上传tips
     * @param $inputData
     * @param $name
     * @return array
     * @throws \Exception
     *
     */
    public function upload($inputData,$name){
        try{
            if(array_key_exists('avatar',$inputData)){
                $file = $inputData['avatar'];
            }else{
                $file = $inputData['file'];
            }
            //原文件名
            $originalName = $file->getClientOriginalName();
            //扩展名
            $ext = $file->getClientOriginalExtension();
            $type = $file->getClientMimeType();
            //临时绝对路径
            $realPath = $file->getRealPath();
            $date = date('Y-m-d',time());
            $filename = uniqid().time().'.'.$ext;
            $thumb_path = public_path('/upload/thumb/');
            $thumb_name =$thumb_path.'/'.$filename;
            $bool = Storage::disk($name)->put($date.'/'.$filename,file_get_contents($realPath));
            $data['url']  = self::MAIN_URL.'/upload/'.$name.'/'.$date.'/'.$filename;
            if (!is_dir($thumb_path)) {
                mkdir($thumb_path,0777,true);
            }
            $imgBig = \Intervention\Image\Facades\Image::make(file_get_contents($realPath));
            $imgBigWidth = $imgBig->width();
            $imgBigHeight = $imgBig->height();
            $imgBig->resize(200*($imgBigWidth/$imgBigHeight), 200)->save($thumb_name);
            $data['thumb'] = self::MAIN_URL.'/upload/thumb/'.$filename;
            return $data;
        }catch(\Exception $e){
            throw $e;
        }
    }

    /**上传头像
     * @param $inputData
     * @return string
     *
     */

    public function uploadHead($inputData){
        $file = $inputData['file'];
        //原文件名
        $originalName = $file->getClientOriginalName();
        //扩展名
        $ext = $file->getClientOriginalExtension();
        $type = $file->getClientMimeType();
        $filename = uniqid().'.'.$ext;
        $head_path = public_path('/upload/head/').date('Y-m-d',time());
        if (!is_dir($head_path)) {
          mkdir($head_path,0777);
        }
        $finalName = $head_path.'/'. $filename;
        //临时绝对路径
        $realPath = $file->getRealPath();
        $imgBig = \Intervention\Image\Facades\Image::make(file_get_contents($realPath));
        $bool=$imgBig->crop($inputData['w'],$inputData['h'],$inputData['x'],$inputData['y'])->save($finalName);
        if(!$bool){
            return '';
        }
        return '/upload/head/'.date('Y-m-d',time()).'/'.$filename;
    }

    /**上传这就是我
     * @param $inputData
     * @return mixed
     * @throws \Exception
     */
    public function uploadMe($inputData){
        try{
            $fileArr = [];
            $meStr= json_decode($inputData['meStr'],true);
            if( $inputData['file1'] && $inputData['file1'] != 'null'){
                $fileArr[0] = $inputData['file1'];
            }
            if( $inputData['file2'] && $inputData['file2'] != 'null'){
                $fileArr[1] = $inputData['file2'];
            }
            if( $inputData['file3'] && $inputData['file3'] != 'null'){
                $fileArr[2]= $inputData['file3'];
            }
            if(count($fileArr) > 0 ){
                foreach($fileArr as $k => $v){
                    $originalName = $v->getClientOriginalName();
                    //扩展名
                    $ext = $v->getClientOriginalExtension();
                    $type = $v->getClientMimeType();
                    //临时绝对路径
                    $realPath = $v->getRealPath();
                    $date = date('Y-m-d',time());
                    $file_path = public_path('/upload/isme/'). $date;
                    $filename = uniqid().time().'.'.$ext;
                    if (!is_dir( $file_path)) {
                        mkdir($file_path,0777);
                    }
                    $imgBig = \Intervention\Image\Facades\Image::make(file_get_contents($realPath));
                    $imgBigWidth = $imgBig->width();
                    $imgBigHeight = $imgBig->height();
                    $imgBig->resize(360, 360*($imgBigHeight/$imgBigWidth))->save( $file_path.'/'.$filename);
                    $meStr[$k]['pictureUrl'] = '/upload/isme/'.$date.'/'. $filename;
                }
            }else{
                if($inputData['operation'] == 2){
                    $meStr = [];
                }
            }
                return $meStr;
        }catch(\Exception $e){
            throw $e;
        }
    }


        public function uploadZip($inputData){
        $zip = $inputData['file'];
        //原文件名
//        $originalName = $zip->getClientOriginalName();
//        //扩展名
//        $ext = $zip->getClientOriginalExtension();
        $type = $zip->getClientMimeType();
        if($type != 'application/zip'){
            return '';
        }
         //临时绝对路径
//        $realPath = $zip->getRealPath();
//        $bool = Storage::disk('zip')->put($filename,file_get_contents($realPath));
        $disk = Storage::disk('oss');
        $date_path = date("Ymd",time());
        $path = $disk->put('/zip/'.$date_path,$zip);
        return config('site.oss_path').$path;
    }


    public static function makeThumb(){
        $img = ThumbImage::make('http://htword.oss-cn-shanghai.aliyuncs.com/img/20221208/63918e8c49170.jpg');
// 压缩图片并保存（保持原始宽度和高度）
// 获取原始图像宽度和高度
        $width = $img->width();
        $height = $img->height();

// 压缩图片并保存（保持原始宽度和高度）
        $img->resize($width, $height, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        })->save(public_path('/upload/img/test_thumb_1.jpg'), 50); // 80 代表图片质量


// 调整图片清晰度并保存（保持原始宽度和高度）
        $img->sharpen(10)->save(public_path('/upload/img/test_thumb_2.jpg'), 50); // 80 代表图片质量
    }

}
